package com.guangwei.oahelp.zwt;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class QueryWeather {

	public static void main(String[] args) throws Exception {
		
		Scanner in = new Scanner(System.in);
		System.out.println("请输入城市!");
		String city = in.nextLine().trim();
		
		String urlStr = "http://api.map.baidu.com/telematics/v3/weather?location="+city+"&output=json&ak=FK9mkfdQsloEngodbFl4FeY3";
		URL url = new URL(urlStr);

		URLConnection con = url.openConnection();

		InputStreamReader isr = new InputStreamReader(con.getInputStream(),
				"utf-8");

		BufferedReader bfreader = new BufferedReader(isr);

		StringBuffer sb = new StringBuffer();

		String line = "";

		while ((line = bfreader.readLine()) != null) {
			sb.append(line);
		}
		//System.out.println(sb.toString());
		JSONObject obj = JSONObject.fromObject(sb.toString());
		//System.out.println(obj.get("date"));
		JSONArray werther = obj.getJSONArray("results");
		//System.out.println(werther);
		JSONObject arr = werther.getJSONObject(0);
		JSONArray wears = arr.getJSONArray("weather_data");
		for(int i=0;i<wears.size();i++){
			JSONObject w = wears.getJSONObject(i);
			System.out.println(w.get("date"));
			System.out.println(w.get("weather"));
			System.out.println(w.get("wind"));
			System.out.println(w.get("temperature"));
			System.out.println("---------------------------------");
		}
	}

}
